﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Hardcodet.Wpf.TaskbarNotification;
using System.Windows.Controls.Primitives;
using Gma.UserActivityMonitor;
using System.Collections;
using System.IO;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Threading;
using WPFTaskbarNotifierExample;

namespace Spremni_List
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        TextBoxOutputter outputter;
        TextBoxOutputterErr outputterErr;
        Spremni_List.poizvedbe xxx;
        private readonly BackgroundWorker worker = new BackgroundWorker();
        ArrayList ena = new ArrayList();
        ArrayList dva = new ArrayList();
        ArrayList tri = new ArrayList();

        ArrayList Patterns = new ArrayList();
        public List<Regex> PatternsReg = new List<Regex>();

        public string TextBoxText
        {
            
            get { return new TextRange(richTextBox2.Document.ContentStart, richTextBox1.Document.ContentEnd).Text; }
            set { this.richTextBox2.DataContext = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            outputter = new TextBoxOutputter(richTextBox2);
            outputterErr = new TextBoxOutputterErr(richTextBox2);
            Console.SetOut(outputter);
            Console.SetError(outputterErr);
            
            checkBox1.IsChecked = true;
            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            xxx = new Spremni_List.poizvedbe(this);
            
            
            //Obvestila.FancyPopup balloon;
  
            //    balloon = new Obvestila.FancyPopup();
            //    balloon.ProcSt = "Kuuuuugbuku";
            //    TaskbarIcon MyNotifyIcon = new TaskbarIcon();
            //    MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Slide, null);
            worker.RunWorkerAsync();
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.Background,
                new Action(() => xxx.getPattern(Patterns) ));
             ena = xxx.getIzdelki();
             dva = xxx.getOperacije();
             tri = xxx.getOperater();
        }
        
        private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            comboBox_izdelek.ItemsSource = ena;   
            comboBox_Operacija.ItemsSource = dva;
            comboBox_Operater.ItemsSource = tri;
        }
        

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Obvestila.Class1 s = new Obvestila.Class1();
            s.getSerialXX("evome");
            //FancyPopup balloon = new FancyPopup();
            //balloon.ProcSt = "Fafek";
            //TaskbarIcon MyNotifyIcon = new TaskbarIcon();
            //MyNotifyIcon.ShowCustomBalloon(balloon, PopupAnimation.Fade, null);
        }


        private void HookManager_KeyPress(object sender, KeyPressEventArgs e)
        {
            richTextBox1.AppendText(e.KeyChar.ToString());
            string textRange = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
            foreach (Regex Pattern in PatternsReg)
            {
                if (Pattern.IsMatch(textRange))
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.Background,
                        new Action(() => xxx.fireCatch(Pattern.ToString(), Pattern.Match(textRange).Value)));
                    richTextBox1.Document.Blocks.Clear();
                }
            }
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            HookManager.KeyPress += HookManager_KeyPress;
        }

        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
            HookManager.KeyPress -= HookManager_KeyPress;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            comboBox_DN.ItemsSource = xxx.getDN();
        }

        private void comboBox_DN_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            textBlock_wbs.Text = xxx.getWbs();
        }

        private void textBox_procesna_input_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)

        {
            if ((e.Key == Key.Enter) || (e.Key == Key.Tab))
            {
                ArrayList neki = xxx.getAllFromProcesna();
                try
                {
                    if (neki[1] != null)
                        comboBox_izdelek.SelectedItem = neki[1];
                    if (neki[0] != null)
                        comboBox_DN.SelectedItem = neki[0];
                }
                catch { }
            }
        }

        private void textBox_procesna_input_GotFocus(object sender, RoutedEventArgs e)
        {
            checkBox1.IsChecked = false;
        }

        private void textBox_procesna_input_LostFocus(object sender, RoutedEventArgs e)
        {
            checkBox1.IsChecked = true;
        }


    }
}
